package StudyCode.summary.mycollection;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class ConvertIterableAsSet {
	public static <T> Set<T> asSet(final Iterable<? extends T> iterable) {
		return (iterable instanceof Collection) ? new HashSet<T>(
				(Collection<? extends T>) iterable) : new HashSet<T>() {
			private static final long serialVersionUID = 3109256773218160485L;

			{
				if (iterable != null) {
					for (final T t : iterable) {
						add(t);
					}
				}
			}
		};
	}

	public static <T> Set<T> asSet(final T t, final T... ts) {
		final Set<T> set = new HashSet<T>(ts.length + 1);
		set.add(t);
		Collections.addAll(set, ts);
		return set;
	}
}
